<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas {
            border: 1px solid red;
        }
    </style>
</head>

<body>
    <canvas width="500" height="500" id="mycanvas"></canvas>
    <script>
        let canvas = document.querySelector("#mycanvas");
        // 获取上下文 ；画笔
        let context = canvas.getContext("2d");

        // 预加载 ：等待资源加载完毕之后再使用 
        function loadImg() {
            return new Promise((resolve, reject) =>{
                let imgEl = document.createElement('img');
                console.log(imgEl);
                imgEl.src = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp02%2F1Z9191923035R0-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1661397561&t=a620034e48d31890e7e652561c79083b";
                imgEl.onload = function() {
                    console.log(this); 
                    resolve(this);
                }
            })
        }

        loadImg().then(imgEl => {
            console.log(imgEl);
            context.drawImage(imgEl, 50, 50, 300, 300);
        },err => {

        })
        // loadImg(function (img) {
        //     context.drawImage(img, 50, 50, 300, 300);
        // })
      


        // context.drawImage(img,50,50,300,300);




    </script>
</body>

</html>